#include "MainWidget.h"
#include "ui_MainWidget.h"

MainWidget::MainWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MainWidget) {
    ui->setupUi(this);
    //初始化设置
    ui->lineEdit_1->setReadOnly(true);
    ui->lineEdit_1->setFocusPolicy(Qt::NoFocus);
    ui->lineEdit_1->setText("255,255,255");
    ui->lineEdit_2->setReadOnly(true);
    ui->lineEdit_2->setFocusPolicy(Qt::NoFocus);
    ui->lineEdit_2->setText("#FFFFFF");
    QIcon pick_color(":/icons/pick_color.ico");
    this->setWindowIcon(pick_color);
    //初始化按钮图标
    QIcon copy_iocn(":/icons/copy.png");
    ui->pushButton_1->setIcon(copy_iocn);
    ui->pushButton_2->setIcon(copy_iocn);
    QIcon pick_icon(":/icons/pick_a.png");
    ui->getColor->setIcon(pick_icon);
    QIcon cut_icon(":/icons/cut.png");
    ui->screenshot_Btn->setIcon(cut_icon);
    QIcon gray_icon(":/icons/gray.png");
    ui->grayImg_Btn->setIcon(gray_icon);
    QIcon binary_icon(":/icons/binary.png");
    ui->binaryImg_Btn->setIcon(binary_icon);

    //获取屏幕长宽
    QScreen *screen = QGuiApplication::primaryScreen();
    this->desktop = new ColorDesktop(nullptr);
    this->desktop->setVisible(false);
    this->desktop->setWidth(screen->size().width());
    this->desktop->setHeight(screen->size().height());
    this->setWindowTitle("WonderfulColor");
    /* 信号 */
    connect(ui->getColor, &QPushButton::clicked, this, &MainWidget::chooseColor);
    connect(this->desktop,&ColorDesktop::clicked,this,&MainWidget::update);
    connect(ui->pushButton_1,&QPushButton::clicked,this,&MainWidget::copyRGB);
    connect(ui->pushButton_2,&QPushButton::clicked,this,&MainWidget::copyHEX);
    connect(ui->binaryImg_Btn,&QPushButton::clicked,this,&MainWidget::makeBinaryImg);
    connect(ui->grayImg_Btn,&QPushButton::clicked,this,&MainWidget::makeGrayImg);
    connect(ui->screenshot_Btn,&QPushButton::clicked,this,&MainWidget::screenShot);
}

MainWidget::~MainWidget() {
    delete ui;
    delete desktop;
}

void MainWidget::chooseColor() {
    this->setVisible(false);
    /* 延时600毫秒 */
    QTime timer = QTime::currentTime().addMSecs(600);
    while( QTime::currentTime() < timer );
    /* 抓取全屏并保存 */
    QPixmap pixmap = QGuiApplication::primaryScreen()->grabWindow(0);
    this->desktop->setImage(pixmap.toImage());
    /* 全屏化展示截图供用户选择点 */
    this->desktop->resize(this->desktop->getWidth(),this->desktop->getHeight());
    this->desktop->setAutoFillBackground(true);
    QPalette palette = this->desktop->palette();
    palette.setBrush(QPalette::Window,
                     QBrush(pixmap.scaled(
                         this->desktop->size(),
                         Qt::IgnoreAspectRatio,
                         Qt::FastTransformation)));
    this->desktop->setPalette(palette);
    this->desktop->showFullScreen();
}

void MainWidget::update() {
    this->setVisible(true);
    //更新按钮状态
    QIcon done_pick_iocn(":/icons/pick_b.png");
    ui->getColor->setIcon(done_pick_iocn);
    //更新文本
    QString RGB = QString("%1,%2,%3")
                      .arg(this->desktop->getColor().red())
                      .arg(this->desktop->getColor().green())
                      .arg(this->desktop->getColor().blue());
    this->ui->lineEdit_1->setText(RGB);
    this->ui->lineEdit_2->setText(this->desktop->getColor().name().toUpper());
    //更新色块
    QString style = "background-color:rgb(";
    style.append(RGB).append(')');
    this->ui->Color->setStyleSheet(style);
    this->ui->Color->setAttribute(Qt::WA_StyledBackground);
}

void MainWidget::copyRGB() {
    QClipboard *clip = QApplication::clipboard();
    clip->setText(this->ui->lineEdit_1->text());
}

void MainWidget::copyHEX() {
    QClipboard *clip = QApplication::clipboard();
    clip->setText(this->ui->lineEdit_2->text());
}

void MainWidget::makeBinaryImg()
{
    // 图像处理
    getOriginal_Img();
    QImage image =originalImage.convertToFormat(QImage::Format_ARGB32);
    QColor oldColor;
    int ts;
    for(int y = 0; y < image.height(); y++)
    {
        for(int x = 0; x < image.width(); x++)
        {
            oldColor = QColor(image.pixel(x,y));
            ts = oldColor.red();
            if(ts<128){
                ts=0;
            }else{
                ts=255;
            }
            image.setPixel(x,y, qRgb(ts, ts, ts));
        }
    }

    // 将处理后的图像复制到剪贴板
    QClipboard *clipboard = QApplication::clipboard();
    clipboard->setImage(image);
    //QMessageBox::information(this, tr("Image Processed"), tr("The image has been processed and copied to the clipboard."));
}



void MainWidget::makeGrayImg()
{
    //获取图像
    getOriginal_Img();
    QImage image =originalImage.convertToFormat(QImage::Format_ARGB32);
    QColor oldColor;

    for(int y = 0; y < image.height(); y++)
    {
        for(int x = 0; x < image.width(); x++)
        {
            oldColor = QColor(image.pixel(x,y));
            int average = (oldColor.red() + oldColor.green() + oldColor.blue()) / 3;
            image.setPixel(x, y, qRgb(average, average, average));
        }
    }
    // 将处理后的图像复制到剪贴板
    QClipboard *clipboard = QApplication::clipboard();
    clipboard->setImage(image);
    //QMessageBox::information(this, tr("Image Processed"), tr("Succeed!"));

}

void MainWidget::screenShot()
{
    getOriginal_Img();
    // 将处理后的图像复制到剪贴板
    QClipboard *clipboard = QApplication::clipboard();
    clipboard->setImage(originalImage);

}
void MainWidget::getOriginal_Img()
{
    // 获取主屏幕的截图
    QScreen *screen = QGuiApplication::primaryScreen();
    if (!screen)
    {
        //状态灯，失败为黄色
        this->ui->state->setStyleSheet("background-color: green;");
        this->ui->state->show();
        return ;
    }
    QPixmap screenshot = screen->grabWindow(0);
    if (screenshot.isNull())
    {
        this->ui->state->setStyleSheet("background-color: green;");
        this->ui->state->show();
        return ;
    }
    QImage originalImage = screenshot.toImage();
    this->originalImage=originalImage;
        //状态灯，成功为绿色
    this->ui->state->setStyleSheet("background-color: green;");
    this->ui->state->show();
}

